﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumDashboard.Library.Models;

namespace ScrumDashboardMvc.Models
{
    public class OverviewViewData
    {
        private OverviewViewData()
        {
        }

        public static OverviewViewData Create(SprintViewData model)
        {
            OverviewViewData stats = new OverviewViewData();

            stats.SprintStart = model.Sprint.SprintStart;
            stats.SprintEnd = model.Sprint.SprintEnd;

            foreach (ProductBacklogItem pbi in model.ProductBacklog)
            {
                stats.TotalStoryPoints += pbi.StoryPoints;
                if (pbi.State == "Done")
                    stats.CompletedStoryPoints += pbi.StoryPoints;
            }

            if (stats.TotalStoryPoints > 0)
                stats.CompletedStoryPointsPercent = 100 * stats.CompletedStoryPoints / stats.TotalStoryPoints;
            else
                stats.CompletedStoryPointsPercent = 0;

            stats.WorkTotal = 0;
            stats.WorkRemaining = 0;
            foreach (ProductBacklogItem pbi in model.ProductBacklog)
            {
                foreach (SprintBacklogTask sbi in pbi.SprintBacklog)
                {
                    stats.WorkRemaining += sbi.WorkRemaining;

                    if (!sbi.EstimatedEffort.HasValue)
                    {
                        continue;
                    }

                    stats.WorkTotal += sbi.EstimatedEffort.Value;
                    if (sbi.IsUnplanned(model.Sprint.SprintStart))
                        stats.ItemsUnplannedWork += sbi.EstimatedEffort.Value;

                    switch (sbi.State)
                    {
                        case "Done":
                            stats.ItemsDone += sbi.EstimatedEffort.Value;
                            break;
                        case "In Progress":
                            stats.ItemsInProgress += sbi.WorkRemaining;
                            break;
                        case "Ready For Test":
                            stats.ItemsReadyForTest += sbi.WorkRemaining;
                            break;
                        case "Not Done":
                            stats.ItemsNotDone += sbi.EstimatedEffort.Value;
                            break;
                    }
                }
            }

            stats.CompletedWork = stats.WorkTotal - stats.WorkRemaining;

            if (stats.WorkTotal > 0)
                stats.CompletedWorkPercent = 100 * stats.CompletedWork / stats.WorkTotal;
            else
                stats.CompletedWorkPercent = 0;

            return stats;
        }

        public DateTime SprintStart;
        public DateTime SprintEnd;

        public double CompletedWork;
        public double CompletedWorkPercent;
        public double WorkTotal;
        public double WorkRemaining;

        public double TotalStoryPoints;
        public double CompletedStoryPoints;
        public double CompletedStoryPointsPercent;

        public double ItemsDone;
        public double ItemsNotDone;
        public double ItemsReadyForTest;
        public double ItemsInProgress;
        public double ItemsUnplannedWork;
    }
}